package com.yygh.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yygh.common.result.Result;
import com.yygh.common.utils.AuthContextHolder;
import com.yygh.enums.OrderStatusEnum;
import com.yygh.model.order.OrderInfo;
import com.yygh.order.service.OrderService;
import com.yygh.order.service.PaymentService;
import com.yygh.order.service.WeixinService;
import com.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/api/order/weixin")
public class WeixinApiControlle {


    @Autowired
    private WeixinService weixinService;

    @Autowired
    private PaymentService paymentService;

    //生成微信支付二维码
    @GetMapping("/createNative/{orderId}")
    public Result createNative(@PathVariable Long orderId) {
        Map map = weixinService.createNative(orderId);
        return Result.ok(map);
    }

    @GetMapping("/queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId) {
        //调用微信接口实现支付状态查询
        Map<String, String> resultMap = weixinService.queryPayStatus(orderId);
        //判断
        if (resultMap == null) {
            return Result.fail().message("支付出错");
        }

        if ("SUCCESS".equals(resultMap.get("trade_state"))) {//如果成功
            //更新订单状态
            String out_trade_no = resultMap.get("out_trade_no");  //订单编码
            paymentService.paySuccess(out_trade_no, resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }


}
